package com.hotai.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hotai.comment.dtos.PageResponseResult;
import com.hotai.comment.dtos.ResponseResult;
import com.hotai.comment.enums.AppHttpCodeEnum;
import com.hotai.comment.md5.MD5Utils;
import com.hotai.comment.pojo.Courseware;
import com.hotai.comment.pojo.Institution;
import com.hotai.comment.pojo.UserStudent;
import com.hotai.comment.pojo.UserTeacher;
import com.hotai.mapper.InstitutionMapper;
import com.hotai.mapper.UserStudentMapper;
import com.hotai.mapper.UserTeacherMapper;
import com.hotai.service.InstitutionSerivice;
import com.hotai.vo.courseware.Coursewaredetaile;
import com.hotai.vo.courseware.CoursewaredetaileList;
import com.hotai.vo.courseware.CoursewaredetaileShe;
import com.hotai.vo.equipment.EquipmentPage;
import com.hotai.vo.institulion.*;
import com.hotai.vo.usestudent.UserStudentList;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class InstitutionSeriviceImpl extends ServiceImpl<InstitutionMapper, Institution> implements InstitutionSerivice {

    @Autowired
    private InstitutionMapper   institutionMapper;

    @Autowired
    private UserStudentMapper userStudentMapper;

    @Autowired
    private UserTeacherMapper  userTeacherMapper;


    private static final String Salt = "MHCDEKLDE321";

    @Override
    public ResponseResult InstitutionPage(InstitutionList query) {
        //参数为空
        if (query == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        query.checkParam();
        PageHelper.startPage(query.getPage(),20);
        ArrayList<Institution> detaile = institutionMapper.InstituionList(query);
        ArrayList<InstituionTear>   instiTear= institutionMapper.InstTear();
        ArrayList<InstituionStudent>  instStudent= institutionMapper.InstStudent();
        for (Institution institution : detaile) {
            for (InstituionTear tear : instiTear) {
                if (institution.getId().equals(tear.getId())){
                    institution.setTeacherNum(tear.getTeacherNum());
                }
            }
            for (InstituionStudent student : instStudent) {
                if (institution.getId().equals(student.getId())){
                    institution.setStudentNum(student.getStudentNum());
                }
            }
        }
        PageInfo<Institution> pageInfo = new PageInfo<>(detaile);
        PageResponseResult responseResult = new PageResponseResult(query.getPage(), pageInfo.getSize(),(int) pageInfo.getTotal());
        responseResult.setData(pageInfo.getList());
        return responseResult;
    }

    @Override
    public ResponseResult Institutiondetail(InstitutionDetile queryware) {
        //参数为空
        if (queryware==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //检查参数
        queryware.checkParam();
        if(queryware.getMethod()==null){
            queryware.setMethod(1);
    }
//        queryware.setPage((queryware.getPage()-1)*queryware.getSize());

        if(queryware.getMethod()==1){
            ArrayList<InstitutiondetaileTea> detaile = institutionMapper.Teadetaile(queryware);
            PageResponseResult responseResult = new PageResponseResult(queryware.getPage()+1,queryware.getSize(), detaile.size());
            responseResult.setData(detaile);
            return responseResult;
        }else if (queryware.getMethod()==2){
            ArrayList<InstitutiondeStu> detaile = institutionMapper.Studetaile(queryware);
            PageResponseResult responseResult = new PageResponseResult(queryware.getPage()+1,queryware.getSize(), detaile.size());
            responseResult.setData(detaile);
            return  responseResult;
        }else if (queryware.getMethod()==3){
            ArrayList<InstitutiondeShe> detaile = institutionMapper.Shedetaile(queryware);
            PageResponseResult responseResult = new PageResponseResult(queryware.getPage()+1,queryware.getSize(), detaile.size());
            responseResult.setData(detaile);
            return  responseResult;
        }else  if (queryware.getMethod()==4){
//            ArrayList<CoursewaredetaileShe> detaile = InstitutionMapper.Studetaile(queryware);
//            PageResponseResult responseResult = new PageResponseResult(queryware.getPage()+1,queryware.getSize(), detaile.size());
//            responseResult.setData(detaile);
//            return  responseResult;
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,AppHttpCodeEnum.SERVER_ERROR.getErrorMessage()) ;
    }

    @Override
    public ResponseResult InstitutionXian(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        try {
            userStudentMapper.UpdateBySId(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            userTeacherMapper.UpdateBySId(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        institutionMapper.UpdateBySId(id)
;        return  ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS,AppHttpCodeEnum.SUCCESS.getErrorMessage());
    }

    @Override
    public ResponseResult Institusave(Institution institution) {
        if (institution.getId()==null){
            try {
                institution.setCreateDate(new Date());
                institution.setDeid(1);
                institution.setStatus(1);
                institution.setAccount(suiji());
                institution.setPassword("000000");
                institutionMapper.insert(institution);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            try {
                institutionMapper.updateById(institution);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return  ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS,AppHttpCodeEnum.SUCCESS.getErrorMessage());



    }

    @Override
    public ResponseResult InstituaddTear(InstitutionAddTear institution) {
        UserTeacher userTeacher = new UserTeacher();
        userTeacher.setCreateDate(new Date());
        userTeacher.setSid(institution.getId());
        userTeacher.setZhename(institution.getZhename());
        userTeacher.setName(suiji());
        userTeacher.setPassword(MD5Utils.encodeWithSalt("000000", Salt));
        userTeacher.setSalt(Salt);
        userTeacher.setDeid(1);
        userTeacher.setStatus(1);
        userTeacherMapper.insert(userTeacher);
        return  ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS,AppHttpCodeEnum.SUCCESS.getErrorMessage());
    }

    @Override
    public ResponseResult InstituaddId(Integer id) {
        try {
            institutionMapper.chongzhi(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS,AppHttpCodeEnum.SUCCESS.getErrorMessage());
    }

    @Override
    public ResponseResult XueTear() {

        return null;
    }

    @Override
    public ResponseResult InstituGao() {
        List<InstitutionSa> institutions = institutionMapper.AddInst();

        return ResponseResult.okResult(institutions);
    }

    @Override
    public ResponseResult InstitutionXuan(String ide) {
        String[] split = ide.split(",");
        for (String s : split) {
            InstitutionXian(Integer.parseInt(s));
        }
        return  ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS,AppHttpCodeEnum.SUCCESS.getErrorMessage());
    }

    @Override
    public ResponseResult InstitutionJie(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        try {
            userStudentMapper.UpdateJieId(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            userTeacherMapper.UpdateJieId(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        institutionMapper.UpdateJieId(id);
        return  ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS,AppHttpCodeEnum.SUCCESS.getErrorMessage());
    }

    private  String  suiji(){
            Random random = new Random();
            String result="";
            for (int i=0;i<10;i++)
            {
                result+=random.nextInt(10);
            }

            return  result;
        }

}
